package nobodyCodesThat;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class TitanQuestGUI {

	private static Singleton instance = Singleton.getInstance();

	public static void main(String[] args) {
		instance.display = new Display();
		createShell();
		instance.shell.open();
		while (!instance.shell.isDisposed()) {
			if (!instance.display.readAndDispatch()) {
				instance.display.sleep();
			}
		}
		instance.display.dispose();
	}

	private static void createShell() {
		instance.shell = new Shell(SWT.CLOSE | SWT.TITLE | SWT.MIN);
		instance.shell.setBounds(
				Display.getCurrent().getBounds().width / 2 - 150, Display
						.getCurrent().getBounds().height / 2 - 100, 320, 270);
		instance.shell.setImage(new Image(Display.getCurrent(),
				Constants.ICON_MAIN));
		instance.shell.setText(Constants.MAIN_TITLE);
		// /////////////////////////////////////////////////////////////
		instance.buttonMain = new Button(instance.shell, SWT.TOGGLE);
		instance.buttonMain.setSelection(true);
		instance.buttonMain
				.setImage(new Image(instance.display, "btn_main.jpg"));
		instance.buttonMain.setBounds(5, 5, 100, 22);
		instance.buttonMain.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				showMainPage(true);
				showRestorePage(false);
				showSettingsPage(false);
			}
		});
		instance.buttonRestore = new Button(instance.shell, SWT.TOGGLE);
		instance.buttonRestore.setImage(new Image(instance.display,
				"btn_restore.jpg"));
		instance.buttonRestore.setBounds(105, 5, 100, 22);
		instance.buttonRestore.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				showMainPage(false);
				showRestorePage(true);
				showSettingsPage(false);
			}
		});
		instance.buttonSettings = new Button(instance.shell, SWT.TOGGLE);
		instance.buttonSettings.setImage(new Image(instance.display,
				"btn_settings.jpg"));
		instance.buttonSettings.setBounds(205, 5, 100, 22);
		instance.buttonSettings.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				showMainPage(false);
				showRestorePage(false);
				showSettingsPage(true);
			}
		});
		createMainPage();
		createRestorePage();
		createSettingsPage();

		// /////////////////////////////////////////////////////////////
		instance.shell.setVisible(true);
	}

	private static void createMainPage() {
		instance.groupMAIN = new Group(instance.shell, SWT.NONE);
		instance.groupMAIN.setBounds(5, 26, 300, 210);
		Label labelProfiles = new Label(instance.groupMAIN, SWT.NONE);
		labelProfiles.setText("Select Your Profile:");
		labelProfiles.setBounds(10, 16, 180, 14);
		instance.tableProfiles = new Table(instance.groupMAIN, SWT.BORDER
				| SWT.SINGLE | SWT.FULL_SELECTION);
		TableColumn columnId = new TableColumn(instance.tableProfiles, SWT.NONE);
		instance.tableProfiles.setBounds(5, 30, 290, 100);
		columnId.setText("Name");
		columnId.setWidth(120);
		columnId.setResizable(false);
		TableColumn columnFile = new TableColumn(instance.tableProfiles,
				SWT.NONE);
		columnFile.setText("Character");
		columnFile.setWidth(150);
		instance.tableProfiles.setHeaderVisible(false);
		instance.tableProfiles.setLinesVisible(false);
		ArrayList<File> myProfiles = FileManagement.getProfilesInDir(System
				.getProperty("user.home")
				+ Constants.TQSAVES);
		File[] profiles = (File[]) myProfiles.toArray(new File[myProfiles
				.size()]);
		for (int i = 0; i < profiles.length; i++) {
			TableItem item = new TableItem(instance.tableProfiles, SWT.NONE);
			item.setText(new String[] {
					"" + profiles[i].getName().substring(1),
					FileManagement.getCharacterType(profiles[i]
							.getAbsolutePath()) });
		}
		instance.tableProfiles.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(SelectionEvent e) {

			}

			public void widgetSelected(SelectionEvent e) {

			}
		});
		instance.groupMainButtons = new Group(instance.groupMAIN,
				SWT.SHADOW_OUT);
		instance.groupMainButtons.setText(Constants.GROUP_BUTTONS);
		instance.groupMainButtons.setBounds(10, 140, 280, 60);
		Rectangle buttonBounds = new Rectangle(10, 20, 100, 24);
		instance.buttonStart = new Button(instance.groupMainButtons, SWT.NONE);
		instance.buttonStart.setText(Constants.START_PROCESS);
		instance.buttonStart.setBounds(buttonBounds);
		instance.buttonStart.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (instance.tableProfiles.getSelectionCount() > 0) {
					instance.copyThread = new CopyThread();
					instance.inputFolder = instance.textInputFolder.getText()
							+ "\\_"
							+ instance.tableProfiles.getSelection()[0]
									.getText(0);
					instance.cycleTime = Integer
							.parseInt(instance.comboCycleTime.getText()) * 60000;
					instance.copyHistory = Integer
							.parseInt(instance.comboCopyHistory.getText());
					instance.copyThread.start();
					instance.buttonStart.setVisible(false);
					instance.buttonEnd.setVisible(true);
					System.out.println("Starting process...\n");
//					AnimationLoader.startAnim();
				} else {
					MessageBox messageBox = new MessageBox(instance.shell,
							SWT.ICON_ERROR | SWT.OK);
					messageBox.setText("Error");
					messageBox
							.setMessage("No profile seleted.\nSelect the profile you are currently using in Titan Quest.");
					messageBox.open();
				}
			}
		});
		instance.buttonEnd = new Button(instance.groupMainButtons, SWT.NONE);
		instance.buttonEnd.setText(Constants.STOP_PROCESS);
		instance.buttonEnd.setVisible(false);
		instance.buttonEnd.setBounds(buttonBounds);
		instance.buttonEnd.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				instance.copyThread.stopThread();
				instance.buttonStart.setVisible(true);
				instance.buttonEnd.setVisible(false);
//				AnimationLoader.stopAnim();
			}
		});
	}

	private static void showMainPage(boolean show) {
		if (show) {
			instance.buttonMain.setSelection(true);
			instance.buttonRestore.setSelection(false);
			instance.buttonSettings.setSelection(false);
		}
		instance.groupMAIN.setVisible(show);
	}

	private static void createRestorePage() {
		instance.groupRESTORE = new Group(instance.shell, SWT.NONE);
		instance.groupRESTORE.setBounds(5, 26, 300, 210);
		instance.groupRESTORE.setVisible(false);
		instance.tableHistorySave = new Table(instance.groupRESTORE, SWT.BORDER
				| SWT.SINGLE | SWT.FULL_SELECTION);
		TableColumn columnId = new TableColumn(instance.tableHistorySave,
				SWT.NONE);
		instance.tableHistorySave.setBounds(5, 12, 290, 121);
		columnId.setText(Constants.TABLE_ID);
		columnId.setWidth(30);
		columnId.setResizable(false);
		TableColumn columnFile = new TableColumn(instance.tableHistorySave,
				SWT.NONE);
		columnFile.setText(Constants.TABLE_FILE);
		columnFile.setWidth(80);
		TableColumn columnName = new TableColumn(instance.tableHistorySave,
				SWT.NONE);
		columnName.setText(Constants.TABLE_ATTRIBUTES);
		if (FileManagement.getOldSaveGamesInDir(System.getProperty("user.dir"))
				.size() > 7) {
			columnName.setWidth(163);
		} else {
			columnName.setWidth(176);
		}
		instance.tableHistorySave.setHeaderVisible(true);
		instance.tableHistorySave.setLinesVisible(true);
		fillRestoreTable();
		instance.tableHistorySave.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(SelectionEvent e) {
				restoreFile();
			}
		});
		instance.groupRestoreButtons = new Group(instance.groupRESTORE,
				SWT.NONE);
		instance.groupRestoreButtons.setBounds(10, 140, 280, 60);
		instance.groupRestoreButtons.setText("Options");
		Button restoreButton = new Button(instance.groupRestoreButtons,
				SWT.NONE);
		restoreButton.setBounds(10, 25, 150, 22);
		restoreButton.setText(Constants.RESTORE_SELECTED);
		restoreButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (instance.tableHistorySave.getSelectionIndex() == -1) {
					System.out.println("Nothing choosed !");
				} else {
					restoreFile();
				}
			}
		});
		// Button closeButton = new Button(groupRestoreButtons, SWT.NONE);
		// closeButton.setBounds(170, 25, 100, 22);
		// closeButton.setText("Close Window");
		// closeButton.addMouseListener(new MouseAdapter() {
		// public void mouseUp(MouseEvent e) {
		//
		// }
		// });
	}

	public static void fillRestoreTable() {
		instance.tableHistorySave.setItemCount(0);
		ArrayList<File> oldSaveGames = FileManagement
				.getOldSaveGamesInDir(System.getProperty("user.dir"));
		File[] oldFiles = (File[]) oldSaveGames.toArray(new File[oldSaveGames
				.size()]);
		for (int i = 0; i < oldFiles.length; i++) {
			long timestamp = oldFiles[i].lastModified();
			Date date = new Date(timestamp);
			SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy  HH:mm:ss");
			String output = sdf.format(date);
			TableItem item = new TableItem(instance.tableHistorySave, SWT.NONE);
			item.setText(new String[] { "" + (i + 1),
					"" + oldFiles[i].getName(), output });
		}
	}

	private static void showRestorePage(boolean show) {
		if (show) {
			fillRestoreTable();
			instance.buttonMain.setSelection(false);
			instance.buttonRestore.setSelection(true);
			instance.buttonSettings.setSelection(false);
		}
		instance.groupRESTORE.setVisible(show);
	}

	private static void restoreFile() {
		MessageBox messageBox = new MessageBox(instance.shell, SWT.ICON_WARNING
				| SWT.CANCEL | SWT.OK);
		messageBox.setText(Constants.WARNING);
		messageBox.setMessage(Constants.WARNING_OVERWRITE);
		if (messageBox.open() == SWT.OK) {
			FileManagement.unzipDir(Constants.HISTORY_FILENAME
					+ instance.tableHistorySave.getSelectionIndex() + ".zip");
		}
	}

	private static void createSettingsPage() {
		instance.groupSETTINGS = new Group(instance.shell, SWT.NONE);
		instance.groupSETTINGS.setBounds(5, 26, 300, 210);
		instance.groupSETTINGS.setVisible(false);
		instance.labelInputFolder = new Label(instance.groupSETTINGS, SWT.NONE);
		instance.labelInputFolder.setText(Constants.SAVE_DIR);
		instance.labelInputFolder.setBounds(new Rectangle(10, 20, 180, 14));
		instance.textInputFolder = new Text(instance.groupSETTINGS, SWT.BORDER);
		instance.textInputFolder.setText(System.getProperty("user.home")
				+ Constants.TQSAVES);
		instance.textInputFolder.setBounds(new Rectangle(10, 36, 255, 20));
		instance.openInputFolder = new Button(instance.groupSETTINGS,
				SWT.BORDER);
		instance.openInputFolder.setImage(new Image(Display.getCurrent(),
				Constants.ICON_FOLDER));
		instance.openInputFolder.setBounds(new Rectangle(266, 36, 24, 20));
		instance.openInputFolder.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				DirectoryDialog dd = new DirectoryDialog(instance.shell);
				dd.setMessage(Constants.CHOOSE_FOLDER);
				dd.setText(Constants.CHOOSE_FOLDER_TITLE);
				dd.setFilterPath(instance.textInputFolder.getText());
				if (dd.open() != null) {
					instance.textInputFolder.setText(dd.open());
				}
			}
		});
		// /////////////////////////////////////////////////////////////
		instance.groupSettingsCombos = new Group(instance.groupSETTINGS,
				SWT.SHADOW_OUT);
		instance.groupSettingsCombos.setText(Constants.SETTINGS);
		instance.groupSettingsCombos.setBounds(new Rectangle(10, 75, 280, 80));
		Label labelCycleTime = new Label(instance.groupSettingsCombos, SWT.NONE);
		labelCycleTime.setText(Constants.CYCLE_TIME);
		labelCycleTime.setBounds(new Rectangle(10, 25, 100, 20));
		instance.comboCycleTime = new Combo(instance.groupSettingsCombos,
				SWT.NONE);
		String[] itemsCycleTime = new String[10];
		for (int i = 0; i < itemsCycleTime.length; i++) {
			itemsCycleTime[i] = (i + 1) + "";
		}
		instance.comboCycleTime.setItems(itemsCycleTime);
		instance.comboCycleTime.setText(itemsCycleTime[3]);
		instance.comboCycleTime.setBounds(new Rectangle(145, 20, 50, 20));
		Label labelCycleTimeUnits = new Label(instance.groupSettingsCombos,
				SWT.NONE);
		labelCycleTimeUnits.setText("Minutes");
		labelCycleTimeUnits.setBounds(205, 25, 60, 20);
		Label labelCopyHistory = new Label(instance.groupSettingsCombos,
				SWT.NONE);
		labelCopyHistory.setText(Constants.COPY_HISTORY);
		labelCopyHistory.setBounds(new Rectangle(10, 55, 100, 20));
		instance.comboCopyHistory = new Combo(instance.groupSettingsCombos,
				SWT.NONE);
		String[] itemsCopyHistory = new String[8];
		for (int i = 0; i < itemsCopyHistory.length; i++) {
			itemsCopyHistory[i] = (i + 1) * 2 + "";
		}
		instance.comboCopyHistory.setItems(itemsCopyHistory);
		instance.comboCopyHistory.setText(itemsCopyHistory[5]);
		instance.comboCopyHistory.setBounds(new Rectangle(145, 50, 50, 20));
		Label labelCopyHistoryTimeUnits = new Label(
				instance.groupSettingsCombos, SWT.NONE);
		labelCopyHistoryTimeUnits.setText("Save Games");
		labelCopyHistoryTimeUnits.setBounds(205, 55, 60, 20);
		// /////////////////////////////////////////////////////////////

	}

	private static void showSettingsPage(boolean show) {
		if (show) {
			instance.buttonMain.setSelection(false);
			instance.buttonRestore.setSelection(false);
			instance.buttonSettings.setSelection(true);
		}
		instance.groupSETTINGS.setVisible(show);
	}

}
